package com.ruoyi.yczx.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.yczx.domain.Yczx;
import com.ruoyi.yczx.domain.YczxFj;
import com.ruoyi.yczx.service.IYczxService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 育成中心Controller
 *
 * @author huacai
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/yczx/yczx")
public class YczxController extends BaseController {
    @Resource
    private IYczxService yczxService;

    /**
     * 查询育成中心列表
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:list')")
    @GetMapping("/list")
    public TableDataInfo list(Yczx yczx) {
        startPage();
        List<Yczx> list = yczxService.selectYczxList(yczx);
        return getDataTable(list);
    }

    /**
     * 导出育成中心列表
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:export')")
    @Log(title = "育成中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Yczx yczx) {
        List<Yczx> list = yczxService.selectYczxList(yczx);
        ExcelUtil<Yczx> util = new ExcelUtil<Yczx>(Yczx.class);
        util.exportExcel(response, list, "育成中心数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Yczx> util = new ExcelUtil<Yczx>(Yczx.class);
        util.importTemplateExcel(response, "育成中心数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "育成中心", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('yczx:yczx:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Yczx> util = new ExcelUtil<Yczx>(Yczx.class);
        InputStream inputStream = file.getInputStream();
        List<Yczx> list = util.importExcel(inputStream);
        inputStream.close();
        int count = yczxService.batchInsertYczx(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取育成中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:query')")
    @GetMapping(value = "/{yczxId}")
    public AjaxResult getInfo(@PathVariable("yczxId") String yczxId) {
        return success(yczxService.selectYczxByYczxId(yczxId));
    }

    /**
     * 新增育成中心
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:add')")
    @Log(title = "育成中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Yczx yczx) {
        yczx.setUserId(Math.toIntExact(getUserId())); //用户ID
        yczx.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        yczx.setCreateBy(getUsername()); //创建人
        return toAjax(yczxService.insertYczx(yczx));
    }

    /**
     * 修改育成中心
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:edit')")
    @Log(title = "育成中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Yczx yczx) {
        return toAjax(yczxService.updateYczx(yczx));
    }

    /**
     * 删除育成中心
     */
    @PreAuthorize("@ss.hasPermi('yczx:yczx:remove')")
    @Log(title = "育成中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{yczxIds}")
    public AjaxResult remove(@PathVariable String[] yczxIds) {
        return toAjax(yczxService.deleteYczxByYczxIds(yczxIds));
    }

    /**
     * 获得育成中心主键ID (UUID)
     */
    @GetMapping("/getYcZxId")
    public AjaxResult getYcZxId() {
        String ycZx = IdUtils.fastSimpleUUID();
        return success(ycZx);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertYczxFjByYczxId(@RequestBody YczxFj yczxFj) {
        return toAjax(yczxService.insertYczxFjByYczxId(yczxFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectYczxFjList(String sId) {
        YczxFj yczxFj = new YczxFj();
        yczxFj.setYczxId(sId);
        return AjaxResult.success(yczxService.selectYczxFjList(yczxFj));
    }

    /**
     * 删除育成中心附件
     */
    @Log(title = "育成中心附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteYczxFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(yczxService.deleteYczxFjByFjIds(fjIds));
    }
}
